<?php

namespace App\Http\Controllers;

use App\Models\AccessToken;
use App\Models\WxUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\User;
use App\Http\Common\Consumer;
use Illuminate\Support\Facades\Cache;

class TestApiController extends Controller
{
    //构造函数
    public function __construct(Request $request)
    {
//        $res = $request->all();
//        $Comsumer = new Consumer();
//        $Comsumer->check_token($res['token']);
    }

    //测试函数
    public function index(Request $request)
    {
        print_r(phpinfo());

        //调用不同的两个数据库 需要配置.env文件
//         $weixin_test = DB::table('weixin_test')->where('content', '内容')->get();
//         print_r($weixin_test);
//         $mysql2 = DB::connection('mysql2')->table('users')->where('name', 'liuyang')->get();
//         print_r($mysql2);

        //调用模型层查询 需要生成并配置models文件
//         $weixin_test = $weixin->where('content', '内容')->get();
//         print_r($weixin_test[1]->content);

//        User::insert(array(
//            'name' => 'songbao',
//            'email' => 'songbao@qq.com',
//            'password' => bcrypt(123123),
//            'created_at' => date('Y-m-d H:i:s'),
//            'updated_at' => date('Y-m-d H:i:s'),
//        ));

        //新增
//        User::insert(array('name' => '梨花','password' => '123456','remember_token' => 'aaabbbccc'));
//        return array('error'=>0,'msg'=>'successful');

        //修改
//        User::where(array('email' => 'luna@qq.com', 'id'=>13))->update(array('name' => 'niming','email'=>'niming@qq.com'));
        //删除
//        User::where('id', '>', 11)->delete();
//        User::where('id', 9)->delete();
        //查询多条
//        $user = DB::table('users')->where('id', '>', 3)->orderBy('id', 'desc')->get();

//        $name = 'xiao';
//        $email = '@qq.com';
//        $user = User::where('id',2)->orwhere('email','like','%'.$email)->orwhere('name','like','%'.$name.'%')->get()->toArray();
        //查询单条
//        $user = User::where('id', 1)->select('id', 'name')->first();
//        echo $user;
//        var_dump($user);

        //模型层查询并传递到视图页
//         $res = $request->all();
//         print_r($res['name']);
//         return view('weixin/index', compact('res'));

        //接收参数
//         if ($request->isMethod('get')) {
//             $name = $request->input('name');
//             print_r($name);

//             //当没有name参数时，默认传递name参数为'匿名'
//             $name = $request->input('name', '匿名');
//             print_r($name);
//         }

//        $res = $request->all();
//        LOG::debug($res);
//        $arr = array('error'=>400,'msg'=>'test is ok');
//        return $arr;

//        $res = $request->all();
//        $user = User::where('id', 2)->select('id', 'name')->first()->toArray();
//        $arr = array('error'=>400,'msg'=>$user,'request'=>$res);
//        LOG::debug($arr);
//        return $arr;

//        $test = test();
//        echo $test;

//        $Comsumer = new Consumer();
//
//        $openid = 'oqDHXwQDGXFOvthhkWHQRkes6rNs';
//        $getWxUserInfo = $Comsumer->getWxUserInfo($openid);
//        var_dump($getWxUserInfo);

//        $token = $Comsumer->getWxAccessToken();
//        var_dump($token);

//        session()->put('access_token', 'access_token');
//        $access_token = session()->get('access_token');
//        echo $access_token;

//        $url = 'http://consumer.test/api/wechat/api';
//        $post_data = array("key1" => 123, "key2" => 456);
//        $data = curl_post($url,$post_data);
//        echo $data;

//        AccessToken::insert(array('access_token' => 'abc123','expire_time' => time()+7000,'update_time' => time()));



//        //返回值数组
//        $de_res = array('msg'=>'', 'error'=> 0, 'data'=> array());
//
////        $redirect_uri = 'https://bi.bluemoon.com.cn/api/testapi/index';
////        $redirect_uri=urlencode($redirect_uri);
////        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . 'wx44020a0c14f0a6e8' . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state='.'STATE'.'#wechat_redirect';
////        header("Location:".$url);
////        LOG::debug($request->all());
//
//        $code = '081WPwKY1HCmFT0QaDKY13GCKY1WPwKU';
//        if(!$code){
//            $de_res['msg'] = '缺少code参数';
//            $de_res['error'] = 400;
//            $en_res = json_encode($de_res, 320);
//            exit($en_res);
//        }
//        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . 'wx44020a0c14f0a6e8' . '&secret=' . 'ae3ee4bb340203196d3d2fdda5913278' . '&code=' . $code . '&grant_type=authorization_code';
//        $access_token_str = curl_get($get_token_url);
//        $access_token_list = json_decode($access_token_str, true);
//        if (!$access_token_list['access_token']) {
//            $de_res['msg'] = $access_token_list['errmsg'];
//            $de_res['error'] = $access_token_list['errcode'];
//            $en_res = json_encode($de_res, 320);
//            exit($en_res);
//        }
//        //获取access_token
//        $access_token = $access_token_list['access_token'];
//        //获取openid
//        $openid = $access_token_list['openid'];
//        //获取微信用户信息
//        $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
//        $user_msg_str = curl_get($get_user_info_url);
//        $user_msg_list = json_decode($user_msg_str, true);
//        var_dump($user_msg_list);


        /*短信接口*/
//        $url = 'https://angelapi.bluemoon.com.cn/moonMall-proxy-web/sms/sendMsgByMobile';//测试
////        $url = 'http://mallapi.bluemoon.com.cn/moonMall-proxy-web/sms/sendMsgByMobile';//正式
//        $headers = array(
//            "Content-type: application/json;charset='utf-8'",
//            "Accept: application/json",
//            "Cache-Control: no-cache",
//            "Pragma: no-cache"
//        );
//        $post_arr = array(
//            'content' => '您好，这是蓝月亮测试短信。',
//            'myAccount' => 'xfzfw',
//            'mobileNo' => '18682003651',
//            'myProjectName' => '消费者服务号',
//            'myRemark' => '电商消费者服务项目调用短信'
//        );
//        $post_json = json_encode($post_arr,true);
//
//        $res = curl_post_bm($url, $post_json);
//
//        print_r($res);


        /*token验证*/
//        print_r(11111111);
//        $Comsumer = new Consumer();
//        $Comsumer->check_token('20190709170325FayKKDFYF9');
//        print_r(22222222);

//        $res = $request->all();
//        print_r($res['token']);

//        $update_token = WxUser::where(array('unionid'=>'opbF4uJrnR7NFnW4TnZJTpMfFeYg'))->select('token','token_at')->first();
//        if(!isset($update_token)){
//            echo 0;
//        }else{
//            echo 1;
//        }
//        Log::debug($update_token);


//        //put 写入缓存
//        Cache::put('key1', 'val1', 1);
//        //add() 添加缓存 成功返回 true 如果键存在返回 false
//        $bool = Cache::add('key2', 'val2', 10);
//        var_dump($bool);
//        //forever 永久缓存, 没有过期时间
//        Cache::forever('key3', 'val3');
//        //使用 has 判断键是否存在
//        if(Cache::has('key4')) {
//            $val = Cache::get('key3');
//            var_dump($val);
//        } else {
//            echo 'No';
//        }

//        //get() 取出缓存
//        $val = Cache::get('key1');
//        var_dump($val);
//        //pull() 取出缓存，并从缓存中移除
//        $val2 = Cache::pull('key2');
//        var_dump($val2);
//        //forget() 从缓存移除 成功返回 true 失败返回 false
//        $bool = Cache::forget('key3');
//        var_dump($bool);
    }
}
